import * as Cesium from 'cesium';
// @ts-ignore
import defaultImage from '@/assets/img/location.png';
import gisTools from '../GisTools';
import mouseHelper from '../MouseHelper';
//当前类使用的全局的viewer
let cviewer: any = {};
//当前类的事件句柄
let handler: any = {};

/**
 * 添加、清除测试过
 */
class DrawPoint {
  isDrawingEnd: any;
  drawedPositions: any;
  drawedGraphics: any;
  constructor(viewer: any) {
    //绘制完成的graphics 数组 Array<any>
    this.drawedGraphics = [];
    //绘制完成的geometry  -数据类型 Array<Cartesian3>
    this.drawedPositions = [];
    //是否绘制结束
    this.isDrawingEnd = false;
    cviewer = viewer;
    handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
  }
  /**
   * 开始绘制
   * @param bNeed  true | false  表示绘制动作是否是连续性的，即绘制完一个不结束，默认false
   * @param options  {
   *   "Point" : { "iconImage":"图片的内容"  }
   *    "popOffset":{x:400,y:0}
   * }
   */
  startDraw(bNeed: boolean=false, options: any={}) {
    this.clearDrawGraphic();
    let _this = this;
    let { iconImage,popOffset } = options;
    if(mouseHelper.helperTips){
      mouseHelper.removeHelper();
    }
    mouseHelper.createHelper({ x:-500,y:-500 }, '',popOffset);
    //左键事件
    handler.setInputAction(function(evt: any) {
      let car3 = gisTools.getCar3Position(cviewer,evt.position);
      let pointEntity = _this.createPoint(car3, iconImage);
      cviewer.scene.requestRender();
      _this.drawedGraphics.push(pointEntity);
      if (!bNeed) { // 不是连续性
        _this.drawedPositions.push(car3);
        _this.isDrawingEnd = true;
        _this.endDrawing();
      } else {
        _this.drawedPositions.push(car3);
      }
    }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
    //鼠标移动事件
    handler.setInputAction(function(evt: any) {
      mouseHelper.updateHelper(evt.endPosition, '点击鼠标左键添加',popOffset);
    }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
  }

  /**
   *结束绘制的事件
   */
  endDrawing() {
    if(handler){
      handler.removeInputAction(Cesium.ScreenSpaceEventType.MOUSE_MOVE);
      handler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK);
    }
    mouseHelper.removeHelper();
    return true;
  }

  /**
   * 清除绘制的Graphic
   */
  clearDrawGraphic() {
    this.endDrawing();
    if(this.drawedGraphics.length>0){
      this.drawedGraphics.forEach((item:any)=>{
        cviewer.entities.remove(item)
      })
    }
    this.isDrawingEnd=false;
    this.drawedPositions=[];
    this.drawedGraphics=[];
    return true;
  }

  /**
   * 添加点Marker
   * @param position 坐标，笛卡尔坐标 Cesium.Cartesian3
   * @param iconImage  图片的内容
   */
  createPoint(position: any, iconImage: string) {
    if (!iconImage) {
      iconImage = defaultImage;
    }
    let currentScale = 0;
    let imageSize = 0.5;
    let point:any=null;
    if(iconImage!=="无"){
       point = cviewer.entities.add({
        position: position,
        billboard: {
          image: iconImage,
          scale: new Cesium.CallbackProperty(() => {
            currentScale += 0.05;
            return currentScale >= imageSize ? imageSize : currentScale;
          }, false),
          disableDepthTestDistance: Number.POSITIVE_INFINITY,
          heightReference: Cesium.HeightReference.RELATIVE_TO_GROUND,
        },
      });
    }else{
      point = cviewer.entities.add({
        position: position,
        point: {
          show: true, //是否显示，默认显示
          pixelSize: 8, //点的大小（像素），默认为1
          // heightReference: Cesium.HeightReference.NONE, //高程参考面：无
          heightReference: Cesium.HeightReference.CLAMP_TO_GROUND, //高程参考面：地表(设置后点会贴地)
          color: Cesium.Color.RED, //颜色，默认为白色
          // outlineColor: Cesium.Color.BLUE, //轮廓线颜色，默认为黑色
          // outlineWidth: 1, // 轮廓线宽度（像素），默认为0
          // scaleByDistance: new Cesium.NearFarScalar(1.5e2, 2.0, 1.5e7, 0.5), //点的大小随视角的距离缩放，默认为空
          // translucencyByDistance: new Cesium.NearFarScalar(1.5e2, 1.0, 1.5e7, 0.2), //点的透明度随视角的距离变化，默认为空
          // distanceDisplayCondition: new Cesium.DistanceDisplayCondition(100.0, 2000000.0), //点在该视角距离内可见，其他距离不可见，默认为空
          // disableDepthTestDistance: 1.5e2, //小于该数值后关闭深度检测，默认为空
      }
      });
    }
    return point;
  }
}
export default DrawPoint;



